//
//  CoachDetailController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/12.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "CoachDetailController.h"
#import "CoachDetailHeaderView.h"
#import "YDWebView.h"
#import "CoachInfoViewController.h"
@interface CoachDetailController ()<WKNavigationDelegate,YDWebViewDelegate>
@property (nonatomic, strong) M_Coach *coach;//
@end

@implementation CoachDetailController

#pragma mark Class Method
+ (instancetype)controllerWithCoach:(M_Coach *)coach
{
    CoachDetailController *vc = [[CoachDetailController alloc] init];
    vc.coach = coach;
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"教练信息";
    [self configUI];
}

#pragma mark UI
- (void)configUI
{
    CoachDetailHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([CoachDetailHeaderView class]) owner:nil options:nil] lastObject];
    [headerView.avaterImageView setAvaterImageWithUrl:_coach.avatarUrl];
    headerView.nameLabel.text = _coach.coachName;
    headerView.mobileLabel.text = _coach.coachMobile;
    headerView.gymNameLabel.text = _coach.gym.gymName;
    [headerView addVCenterImageViewKeepRight:20 width:6 height:10 imageName:@"ic_more"];
    UITapGestureRecognizer *headerViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headerViewClicked)];
    [headerView addGestureRecognizer:headerViewTap];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(10);
        make.height.equalTo(@(95));
    }];
    
    YDWebView *webView = [[YDWebView alloc] init];
    [webView.bridge setWebViewDelegate:self];
    webView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom).offset(10);
    }];
    
    NSString *url_route = [NSString stringWithFormat:@"/manage/dist/index.html#/coachMsg/%@",_coach.coachId];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:Url_ForCoachInfo(url_route)]]];
}


#pragma mark Action
- (void)headerViewClicked
{
    CoachInfoViewController *coachInfoVC = [CoachInfoViewController controllerWithCoach:_coach];
    [self.navigationController pushViewController:coachInfoVC animated:YES];
}


#pragma mark - WKWKNavigationDelegate Methods

/*
 *5.在WKWebViewd的代理中展示进度条，加载完成后隐藏进度条
 */

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [webView showLoading];
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [webView hideStatusView];
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [webView showError:error refreshBlock:^{
        [webView reload];
    }];
}


@end
